package com.yx.learn.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yx.learn.domain.Authority;
import com.yx.learn.domain.User;
import com.yx.learn.domain.UserAuthority;
import com.yx.learn.service.AuthorityService;
import com.yx.learn.service.UserAuthorityService;
import com.yx.learn.service.UserService;

@Controller("userAuthorityAction")
@Scope("prototype")
public class UserAuthorityAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private UserAuthorityService userAuthorityService;
	@Resource
	private UserService userService;
	@Resource
	AuthorityService authorityService;
	

	private int uid;
	private int id;
	private String aids;
	private List<Authority> authHad;
	
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();

	// 获得当前角色全部权限
		public void getAllUserAuthority() {

			try {
				authHad = userAuthorityService.getAuthorityByUid(uid);
				if (session.getAttribute("authHad") != null) {
					session.removeAttribute("authHad");
				}
				session.setAttribute("authHad", authHad);
				JsonConfig jcf = new JsonConfig();
				jcf.setExcludes(new String[] { "userauthorities",
						"groupauthorities", "roleauthorities" });
				jcf.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
				JSONArray ja = JSONArray.fromObject(authHad, jcf);
				toResponse(ja.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		@SuppressWarnings("unchecked")
		public void addUserAuthority(){
			try {
				List<Integer> listKey = new ArrayList<Integer>();// 获取前台选中的权限
				List<Integer> hasAid = new ArrayList<Integer>();
				List<Integer> delete = new ArrayList<Integer>();
				if (aids.length() > 0) {
					String[] ids1 = aids.split(",");
					for (int i = 0; i < ids1.length; i++) {
						int key = Integer.parseInt(ids1[i]);
						System.out.println("key=" + key);
						listKey.add(key);
					}
				}
				authHad = (List<Authority>) session.getAttribute("authHad");
				for (int i = 0; i < authHad.size(); i++) {
					hasAid.add(authHad.get(i).getId());
				}
				System.out.println("原来有的hasAid：" + hasAid);
				delete.addAll(hasAid);
				if (listKey != null) {
					delete.removeAll(listKey);
					listKey.removeAll(hasAid);
				}
				System.out.println("add：" + listKey);
				System.out.println("delete：" + delete);
				if (listKey != null) {
					for (int i = 0; i < listKey.size(); i++) {
						Authority authority = authorityService.findById(listKey
								.get(i));
						User user = userService.findById(uid);
						UserAuthority userAuthority = new UserAuthority();
						userAuthority.setAuthority(authority);
						userAuthority.setUser(user);
						userAuthority.setMemo("");
						userAuthority.setExpiredtime(new Date());
						userAuthorityService.saveUserAuthority(userAuthority);
					}

				}
				if (delete != null) {
					for (int i = 0; i < delete.size(); i++) {
						userAuthorityService.deleteUserAuthority(userAuthorityService.findUserAuthority(uid, delete.get(i)));
									
					}
				}
				this.toResponse("{\"status\":\"ok\" , \"message\":\"用户授权成功!\"}");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.toResponse("{\"status\":\"fail\" , \"message\":\"用户授权失败!\"}");
			}

		}
		

	

	
	
	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAids() {
		return aids;
	}

	public void setAids(String aids) {
		this.aids = aids;
	}

	

}
